import { computed } from 'vue'
import { useUserStore } from '@/stores/modules/user'

/**
 * 权限检查 Composable
 * 提供统一的权限检查逻辑
 */
export function usePermission() {
  const userStore = useUserStore()
  
  // 响应式的权限数据
  const userHasPermissions = computed(() => userStore.getPermissions)
  const userActions = computed(() => userStore.getActions)
  
  /**
   * 检查用户是否拥有指定权限
   * @param mark 权限标识，格式：'module@Controller@action'
   * @returns boolean 是否拥有权限
   */
  const hasPermission = (mark: string): boolean => {
    if (!userHasPermissions.value || userHasPermissions.value.length === 0) {
      console.warn(`权限检查失败 - 用户权限为空: ${mark}`)
      return false
    }
    
    // 方法1：检查原始权限数据 (推荐，符合CatchAdmin文档标准)
    for (const item of userHasPermissions.value) {
      const permissionKey = item.module + '@' + item.permission_mark
      if (permissionKey === mark) {
        console.log(`权限匹配成功(原始格式): ${mark}`)
        return true
      }
    }
    
    // 方法2：检查actions数组格式 (备用兼容)
    if (userActions.value && userActions.value.length > 0) {
      const actionFormat = mark.replace(/@/g, '.').toLowerCase()
      if (userActions.value.includes(actionFormat)) {
        console.log(`权限匹配成功(Actions格式): ${mark} -> ${actionFormat}`)
        return true
      }
    }
    
    console.log(`权限检查失败: ${mark}`)
    return false
  }
  
  /**
   * 批量检查权限
   * @param marks 权限标识数组
   * @returns boolean 是否拥有所有权限
   */
  const hasAllPermissions = (marks: string[]): boolean => {
    return marks.every(mark => hasPermission(mark))
  }
  
  /**
   * 检查是否拥有任一权限
   * @param marks 权限标识数组
   * @returns boolean 是否拥有任一权限
   */
  const hasAnyPermission = (marks: string[]): boolean => {
    return marks.some(mark => hasPermission(mark))
  }
  
  /**
   * 获取用户所有权限的完整标识
   * @returns string[] 权限标识数组
   */
  const getAllPermissionKeys = (): string[] => {
    if (!userHasPermissions.value) return []
    
    return userHasPermissions.value.map(item => 
      item.module + '@' + item.permission_mark
    )
  }
  
  /**
   * 打印权限调试信息
   * @param pageName 页面名称
   * @param requiredPermissions 当前页面需要的权限
   */
  const debugPermissions = (pageName: string, requiredPermissions: string[]) => {
    console.group(`🔐 权限调试信息 - ${pageName}`)
    console.log('用户所有权限:', userHasPermissions.value)
    console.log('用户可执行操作:', userActions.value)
    
    if (userHasPermissions.value && userHasPermissions.value.length > 0) {
      console.log('权限标识映射:')
      userHasPermissions.value.forEach(item => {
        const permissionKey = item.module + '@' + item.permission_mark
        console.log(`  ${permissionKey} (${item.permission_name || '未命名'})`)
      })
    }
    
    console.log('当前页面需要的权限:')
    requiredPermissions.forEach(permission => {
      console.log(`  - ${permission}`)
    })
    
    console.log('权限检查结果:')
    requiredPermissions.forEach(permission => {
      console.log(`  - ${permission}:`, hasPermission(permission))
    })
    
    console.groupEnd()
  }
  
  return {
    // 数据
    userHasPermissions,
    userActions,
    
    // 方法
    hasPermission,
    hasAllPermissions,
    hasAnyPermission,
    getAllPermissionKeys,
    debugPermissions
  }
} 